var config = require("./config");

//app.js
App({
  globalData: {
    userInfo: {},
    bob_id: '',
    openid: ''
  },
  onLaunch: function () {
    var that = this;
    try {
      var bob_id = wx.getStorageSync('bob_id');
      console.log("bob_id: ", bob_id);
      if (bob_id != '') {
        // 取到了，就说明是注册用户，可以用这个bob_id去服务器取信息
        that.globalData.bob_id = bob_id;
        // 访问服务器接口，获取用户信息
        wx.request({
          url: config.urls.user_one,
          data: {
            id: bob_id
          },
          success: function (res) {
            console.log(res);
            if (res.data.success) {
              that.globalData.userInfo = res.data.user;
              wx.setStorageSync('bob_id', res.data.user.id);
            }
            else {
              // 没找到这个用户，应该再走下面一样的流程
              that.login_register();
            }
          }
        });
      }
      else {
        console.log("bob_id is empty.");
        // 没有取到，走下面的流程
        // 使用 wx.login() 返回的code去服务器获取openid
        that.login_register();
      }
    } catch (e) {
      console.log("no bob_id.");
      // 没有取到，走下面的流程
      // 使用 wx.login() 返回的code去服务器获取openid
      that.login_register();
    }
  },
  
  login_register: function () {
    var that = this;
    wx.login({
      success: function (res) {
        var code = res.code;
        console.log("user login code: ", code);
        wx.request({
          url: config.urls.code,
          data: {
            code: res.code
          },
          success: function (res) {
            console.log(res);
            if (res.data.success) {
              //var session = eval("(" + res.data.session + ")");
              var session = JSON.parse(res.data.session);
              var openid = session.openid;
              that.globalData.openid = openid;
              // 访问服务器接口，获取用户信息
              wx.request({
                url: config.urls.one_by_openid,
                data: {
                  openid: openid
                },
                success: function (res) {
                  console.log(res);
                  if (res.data.success) {
                    that.globalData.userInfo = res.data.user;
                    wx.setStorageSync('bob_id', res.data.user.id);
                  }
                  else {
                    // 没找到这个用户，应该再走下面一样的流程
                    wx.getUserInfo({
                      success: function (res) {
                        var userInfo = res.userInfo;
                        wx.request({
                          url: config.urls.user_new,
                          method: "POST",
                          header: {
                            'content-type': 'application/x-www-form-urlencoded'
                          },
                          data: {
                            openid: openid,
                            nickname: userInfo.nickName,
                            avatar: userInfo.avatarUrl,
                            city: 1
                          },

                          success: function (res) {
                            if (res.data.success) {
                              that.globalData.userInfo = res.data.user;
                              wx.setStorageSync('bob_id', res.data.user.id);
                            }
                            else {
                              console.log("User register failed.");
                            }
                          }
                        });
                      },
                      fail: function (res) {
                        // 用户拒绝授权
                        // 按道理，这个是要处理的。但好像没什么事情可做，后面判断直接通过globalData.userInfo.id 看这个是不是空的就行了

                      }
                    })
                  }
                }
              });
            }
          }
        });
      }
    })
  }


})